//
//  SelectOpenCartAddrVC.m
//  NtoccShipper
//
//  Created by zcj on 2021/2/27.
//  Copyright © 2021 zhuchongjin. All rights reserved.
//

#import "SelectOpenCartAddrVC.h"
#import "SelectBankModel.h"
#import "SelectOpenCartAddrCell.h"
@interface SelectOpenCartAddrVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    FlexFrameView *header;
}
@property (nonatomic,strong) SelectOpenCartAddrCell *selectOpenCartAddrCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) CJTextField *fieldSerach;
@end

@implementation SelectOpenCartAddrVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"选择开户网点"];
    [self setBackLeftButton:@""];
    [self createTopSearchView];
    self.selectOpenCartAddrCell = [[SelectOpenCartAddrCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
    
}
- (void)createTopSearchView{
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"SelectBankCartSeachView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    self.fieldSerach.delegate = self;
    self.fieldSerach.placeholder = @"请填写银行卡名称";
    [header layoutIfNeeded];
    [self.view addSubview:header];
}
- (void)btnSerachRouteView{
    if ([CJRegular getIsStrEmpty:self.fieldSerach.text]) {
        self.fieldSerach.text = @"";
    }
    [self.tableView.mj_header beginRefreshing];
}

- (void)textFieldDidChangeSelection:(UITextField *)textField{
    
    if ([CJRegular getIsStrEmpty:self.fieldSerach.text]) {
        self.fieldSerach.text = @"";
    }
    [self.tableView.mj_header beginRefreshing];
}
- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
    self.fieldSerach.text = @"";
}

/// <#Description#>
- (void)loadData {

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/bankCard/getBankAssociationNumber",CJBaseUrl);
    NSDictionary *dicPra = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"20",
                             @"bankCode":self.bankCode,
                             @"bankName":[CJControl getThreeUnaryValueStr:self.fieldSerach.text]
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {

           if ([ws.refreshType isEqualToString:@"down"]) {
               [ws.modelArray removeAllObjects];
           }

           NSError * error ;
           NSArray *arr = [SelectBankModel arrayOfModelsFromDictionaries:list error:&error];
           [ws.modelArray addObjectsFromArray:arr];

           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshing];

           if (ws.modelArray.count == total.intValue) {
               [ws.tableView.mj_footer endRefreshingWithNoMoreData];
           }

//        }else{
//           [ws.modelArray removeAllObjects];
//           [ws.tableView.mj_header endRefreshing];
//           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        [ws.tableView reloadData];


    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"SelectOpenCartAddrCell";
    SelectOpenCartAddrCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SelectOpenCartAddrCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    SelectBankModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectBankModel *model = self.modelArray[indexPath.row];
    NSDictionary *dic = @{
        @"bankNo":[CJControl getThreeUnaryValueStr:model.bankNo],
        @"bankName":[CJControl getThreeUnaryValueStr:model.bankName]
    };
    
    if (self.blockAddrInfo) {
        self.blockAddrInfo(dic);
    }
    [self pop];
}



#pragma mark ----------lazy
- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight + header.height, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight - header.height),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}


@end
